package com.zjitc.controller;

import com.zjitc.model.Cart;
import com.zjitc.model.Cat;
import com.zjitc.model.User;
import com.zjitc.service.ManageService;
import com.zjitc.service.UserService;

import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;


import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/23 16:39
 * @description:
 */
@Controller
@RequestMapping("/manage")
public class ManageController {

  @Autowired
  private ManageService manageService;
  private UserService userService;

  //@RequestMapping(value = "index")
//public String index(){
//
//}

  @RequestMapping(value = "/findAllUser")
  public ModelAndView findAllUser(HttpSession session) {

    ModelAndView mv = new ModelAndView();
    User user0 = (User) session.getAttribute("user");
    if (user0 == null) {
      String msg = "用户未登录，请先登录。。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
    }else {
      List list = manageService.findAllUser();
      if (list != null) {
        mv.addObject("UserList", list);
      } else {
        mv.setView(new InternalResourceView("404.jsp"));
        return mv;
      }
      mv.setView(new InternalResourceView("userList.jsp"));
    }
    return mv;
  }

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public ModelAndView login(@RequestParam("username") String username,
      @RequestParam("password") String password, HttpSession session) {
    String msg = null;
    ModelAndView mv = new ModelAndView();
    User user = manageService.manageLogin(username, password);
    int count = manageService.checkUser(username);
    if (count != 1) {
      msg = "用户名不存在";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    }
    User user1 = manageService.checkByUsernameAndPassword(username);
    if (user1.getPermission()!=1){
      msg = "用户权限不足，不可进入管理页面";
      mv.addObject("msg",msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    }else if (password.equals(user1.getPassword())) {
      session.setAttribute("user", user);
      mv.setView(new InternalResourceView("index.jsp"));
      return mv;
    } else {
      msg = "登录密码错误，请重新输入";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    }

  }

  @RequestMapping(value = "/findUserInfo")
  public ModelAndView findUserInfo(String username) {
    ModelAndView mv = new ModelAndView();
    String msg = null;
    if (username == "") {
      msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
    } else {
      User user = manageService.findUserInfo(username);
      mv.addObject("user", user);
      mv.setView(new InternalResourceView("personInfo.jsp"));
    }
    return mv;
  }

  @RequestMapping(value = "/findCatById")
  public String findUserById(@RequestParam("id") Integer id, Model model, HttpSession session) {
    User user = (User) session.getAttribute("user");

    if (user == null) {
      String msg = "用户未登录，请先登录。。登陆后按F5刷新网页";
      model.addAttribute("msg", msg);
      return "/manage/login";
    }
    Cat cat = manageService.findCatById(id);
    model.addAttribute("cat", cat);
    return "manage/Add";
  }

  @RequestMapping(value = "/editCat")
  public ModelAndView editUser(@RequestParam("id") int id, String catName, String sex,
      String catSpecies,
      String image, Model model) {
    ModelAndView mv = new ModelAndView();
    String msg = null;
    manageService.editCat(catName, sex, catSpecies, image, id);
    msg = "修改成功";
    mv.addObject("msg", msg);
    String c = catSpecies;
    mv.setView(new InternalResourceView("findCatByCatSpecies.do"));
    return mv;
  }

  @RequestMapping(value = "/editManage")
  public ModelAndView editManage(
      @RequestParam(value = "username", required = false) String username,
      @RequestParam(value = "email", required = false) String email,
      @RequestParam(value = "phone", required = false) String phone) {
    manageService.editManage(username, email, phone);
    User user = manageService.findUserInfo(username);
    ModelAndView mv = new ModelAndView();
    String msg = null;
    msg = "用户信息修改成功";
    mv.addObject("user", user);
    mv.addObject("msg", msg);
    mv.setView(new InternalResourceView("personInfo.jsp"));
    return mv;
  }


  @RequestMapping(value = "/changePwd")
  public String changePwd(@RequestParam("password") String password,
      @RequestParam("newPassword") String newPassword, Model model, HttpSession session) {
    User user0 = (User) session.getAttribute("user");

    if (user0 == null) {
      String msg = "用户未登录，请先登录。。登陆后按F5刷新网页";
      model.addAttribute("msg", msg);
      return "/manage/login";
    }
    User user = manageService.checkOldPwd(password);
    String msg = null;
    if (user != null) {
      manageService.changePwd(newPassword, user.getUsername());
      msg = "更改密码成功,请重新登录";
      User user1 = manageService.findUserInfo(user.getUsername());
      model.addAttribute("user", user1);
      model.addAttribute("msg", msg);
      return "manage/login";
    }
    msg = "原密码输入错误，请重新登录后再试";
    model.addAttribute("msg", msg);
    return "manage/login";
  }

  @RequestMapping(value = "/findAllCat")
  public ModelAndView findAllCat(HttpSession session) {
    ModelAndView mv = new ModelAndView();
    List list = manageService.findAllCat();
    User user0 = (User) session.getAttribute("user");
    if (user0 == null) {
      String msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    } else if (list != null) {
      mv.addObject("CatList", list);
    }
    mv.setView(new InternalResourceView("cartList.jap"));
    return mv;
  }

  @RequestMapping(value = "/deleteUser")
  public ModelAndView deleteUser(@RequestParam("id") Integer id, HttpSession session) {
    ModelAndView mv = new ModelAndView();
    User user0 = (User) session.getAttribute("user");
    int count = 0;
    String username = manageService.UserById(id);
    count = manageService.checkOrderState(username);
    if (user0 == null) {
      String msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
    } else if (id == null) {
      String msg = "用户不存在 无需删除";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("findAllUser.do"));
    } else if (count != 0) {
      String msg = "用户存在订单，无法删除";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("findAllUser.do"));
    } else {
      String msg = "用户删除成功";
      mv.addObject("msg", msg);
      manageService.deleteUser(id);
      mv.setView(new InternalResourceView("findAllUser.do"));
    }
    return mv;
  }

  @RequestMapping(value = "/deleteCat")
  public ModelAndView deleteCat(@RequestParam("id") Integer id,
      @RequestParam("catSpecies") String catSpecies, HttpSession session) {
    ModelAndView mv = new ModelAndView();
    User user0 = (User) session.getAttribute("user");
    if (user0 == null) {
      String msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    } else if (id == null) {
      String msg = "猫咪不存在 无需删除";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("findAllUser.do"));
      return mv;
    } else {
      manageService.deleteCat(id);
      Map map = new HashMap();
      try {
        String c = java.net.URLEncoder.encode(catSpecies, "UTF-8");
        ModelAndView mv1 = new ModelAndView("redirect:findCatByCatSpecies.do?catSpecies=" + c);
        System.out.println(mv1);
        return mv1;
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
/*      mv.setView(new InternalResourceView("redirect:findCatByCatSpecies.do?catSpecies=" + catSpecies));*/
    }

    return mv;
  }

  @RequestMapping(value = "/findCatByCatSpecies")
  public ModelAndView findCatByCatSpecies(@RequestParam("catSpecies") String catSpecies,
      HttpSession session) {
    ModelAndView mv = new ModelAndView();
    User user0 = (User) session.getAttribute("user");
    if (user0 == null) {
      String msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
      return mv;
    }

    List list = manageService.findCatByCatSpecies(catSpecies);
    mv.addObject("CartList", list);
    mv.setView(new InternalResourceView("catList.jsp"));
    return mv;
  }

  @RequestMapping(value = "/cartOrder")
  public ModelAndView cartOrder(HttpSession session) {
    ModelAndView mv = new ModelAndView();
    User user0 = (User) session.getAttribute("user");
    if (user0 == null) {
      String msg = "用户未登录，请先登录。登陆后按F5刷新网页";
      mv.addObject("msg", msg);
      mv.setView(new InternalResourceView("login.jsp"));
    } else {
      List<Cart> allCartList = manageService.findAllcartOrder();
      mv.addObject("allOrderList", allCartList);
      mv.setView(new InternalResourceView("orderList.jsp"));
    }
    return mv;
  }

  @RequestMapping(value = "/changeState")
  public ModelAndView changeState(String uuid) {
    manageService.changeState(uuid);
    ModelAndView mv = new ModelAndView();
    mv.setView(new InternalResourceView("cartOrder.do"));
    return mv;
  }

  @RequestMapping(value = "/query.html", method = RequestMethod.POST)
  @ResponseBody
  protected Object query(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    User user = new User();
    user.setUsername("test");
    if (true) { throw new SQLException("hello error"); }
    return user;
  }

}
